package com.gss.armory.validator;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.multipart.MultipartFile;

import com.gss.armory.form.DocRenewalDetailsForm;
import com.gss.common.util.ApplicationConstants;

@Component
public class RenewalDetailsFormValidator {
	
	Logger logger = Logger.getLogger(RenewalDetailsFormValidator.class);
	
	public void validateViewDocDetailsforRenew(DocRenewalDetailsForm docDetailsForm,Errors errors)
	{
		logger.debug("inside method validateUploadNewDoc");
		logger.debug("number of binding errors: " + errors.getErrorCount());
		for(FieldError fieldError : errors.getFieldErrors()){
			logger.debug(fieldError.toString());
		}
		
		Date startDate = docDetailsForm.getDocValidityStartDate();
		boolean renewable = docDetailsForm.isRenewable();
		if(renewable){		
			Date endDate = docDetailsForm.getDocValidityEndDate();
				if(null == endDate)
					errors.rejectValue("docValidityEndDate", "blankEndDate", null, "End Date cannot be left blank");
			else if(startDate.getTime() > endDate.getTime()){
				errors.rejectValue("docValidityStartDate", "invalidStartDate", null, "Start Date has to be before End Date");
			}else if(endDate.before(new Date())){
				errors.rejectValue("docValidityEndDate", "blankEndDate", null, "End Date cannot be before today's date.");
			}
			int firstEscalationPeriod = docDetailsForm.getFirstEscalationPeriod();
			int secondEscalationPeriod = docDetailsForm.getSecondEscalationPeriod();
			int thirdEscalationPeriod = docDetailsForm.getThirdEscalationPeriod();
			if(thirdEscalationPeriod>=secondEscalationPeriod)
				errors.rejectValue("secondEscalationPeriod", "wrongEscalationPeriod", null, "Second Escalation Period cannot be Less than or Equal to Third Escalation Period");
			if(secondEscalationPeriod>=firstEscalationPeriod)
				errors.rejectValue("firstEscalationPeriod", "wrongEscalationPeriod", null, "First Escalation Period cannot be Less than or Equal to Second Escalation Period");
		}
		if(null == startDate)
			errors.rejectValue("docValidityStartDate", "blankStartDate", null, "Start Date cannot be left blank");
		int docTypeId = docDetailsForm.getDocTypeId();
		if(docTypeId==0){
			errors.rejectValue("docTypeId", null, null, "Please Select a valid Document Type");
		}		
		MultipartFile file = docDetailsForm.getDocContent();
		String fileType = file.getContentType();
		logger.debug(fileType + " --- " + file.getSize());
		if(!fileType.equalsIgnoreCase("application/pdf") && file.getSize() > 0){
			logger.debug(fileType);
			errors.rejectValue("docContent", "invalidImageContentType", null, "Only PDF Files can be Uploaded");
			docDetailsForm.setDocContent(null);
		}

		if(file.getSize() > ApplicationConstants.IMAGE_FILE_SIZE_LIMIT){
			errors.rejectValue("docContent", "invalidImageContentLength", null, "File size exceeded");
		}
		if(docDetailsForm.getDocId() == 0){
			if(file.getSize() == 0){
				errors.rejectValue("docContent", "", null, "Attachment is missing.");
			}
		}
		if(errors.getErrorCount()>0)
		{
			docDetailsForm.setDocContent(null);
		}
		docDetailsForm.setErrorCount(errors.getErrorCount());
	}
	
	public void validateChangeVersion(DocRenewalDetailsForm docDetailsForm,Errors errors)
	{
		logger.debug("inside method validateChangeVersion");
		logger.debug("number of binding errors: " + errors.getErrorCount());
		MultipartFile file = docDetailsForm.getDocContent();
		String fileType = file.getContentType();
		logger.debug(fileType + " --- " + file.getSize());
		if(!fileType.equalsIgnoreCase("application/pdf") && file.getSize() > 0){
			logger.debug(fileType);
			errors.rejectValue("docContent", "invalidImageContentType", null, "Only PDF Files can be Uploaded");
			docDetailsForm.setDocContent(null);
		}

		if(file.getSize() > ApplicationConstants.IMAGE_FILE_SIZE_LIMIT){
			errors.rejectValue("docContent", "invalidImageContentLength", null, "File size exceeded");
		}
		if(file.getSize() == 0){
			errors.rejectValue("docContent", "", null, "Attachment is missing.");
		}
		if(errors.getErrorCount()>0)
		{
			docDetailsForm.setDocContent(null);
		}
		docDetailsForm.setErrorCount(errors.getErrorCount());
	}

}
